import cTitle from "components/title";
import { Toast } from "vant";
export default {
  data() {
    return {
      collectIndex: 0,
      collectStoreIs: false,
      collectTabs: [
        {
          name: "商品",
          status: 1
        },
        {
          name: "商户",
          status: 2
        }
      ],
      commercialList: [],
      isLoadMore: true,
      page: 1,
      totalPage: 0,
      goodsData: [],
      toi: this.fun.getKeyByI(),
      checkList: [],
      datas: [],
      collectIdArr:[],
      commIdArr:[],
      checkAll: false,
      collChecked:false

    };
  },

  activated() {
    this.initData();
    //获取数据
    this.getData();
  },
  mounted() {
    this.toi = this.fun.getKeyByI();
  },

  methods: {
    initData() {
      this.checkAll = false;
      this.collChecked = false;
      this.toi = this.fun.getKeyByI();
      this.datas = [];
      this.collectIdArr = [];
    },
    operateOn() {
      
      this.checkAll = !this.checkAll;
    },
    //全选功能
    collCheckedOn() {
      if(this.collectIndex === 0) {
        this.datas.map((item,index) => {
          item.checked = !item.checked;
        })
      } else {
        this.commercialList.map((item,index) => {
          item.checked = !item.checked;
        })
      }
    },
    confrimOn() {
      if(this.collChecked) {
        this.delteteItem();
      } else {
        if(this.collectIndex === 0) {
          this.collectIdArr.length > 0?this.delteteItem():this.$toast('请选择商品');
        } else {
          this.commIdArr.length > 0?this.delteteItem():this.$toast('请选择商品');
        }
      }
    },
    checkItem(index) { //选择删除
      let collectIdArr = [];
      let commIdArr = [];
      let collArr = []
      if(this.collectIndex === 0) {
        collArr = this.datas;
        this.datas.map((item,index) => {
          if(item.checked) {
            collectIdArr.push(item.goods?.id || '');
          }
        })
        this.collectIdArr = collectIdArr;
      } else {
        collArr = this.commercialList;
        this.commercialList[index].checked = !this.commercialList[index].checked;
        this.commercialList.map((item,index) => {
          if(item.checked) {
            commIdArr.push(item.id || '');
          }
        })
        this.commIdArr = commIdArr;
      }
      this.collChecked = collArr.every((item) => item.checked);
    },
    //获取数据
    getData() {
      let that = this;
      let json = { i: this.fun.getKeyByI(), type: this.fun.getTyep(), basic_info: 1 };

      $http.get("member.member-favorite.index", json).then(
        function(response) {
          if (response.result == 1) {
            that.datas = response.data;
            that.datas.map((item,index) => {
              that.$set(item, 'checked', false);
            });
            let couponStore = response.basic_info.plugin_setting?.coupon_store;
            if (Object.prototype.toString.call(couponStore) === "[object Object]") {
              that.collectStoreIs = true;
              that.storeCollectList();
            }
          }
        },
        function(response) {
          console.log(response);
        }
      );
    },
    onStoreChange(event) {
      //切换商户
      this.checkAll = false;
      this.collChecked = false;
      this.collectIndex = event;
    },
    storeCollectList() {
      let json = {
        basic_info: 1
      };
      let that = this;
      $http.get("plugin.coupon-store.frontend.collection.storeCollectList", json, "处理中...").then(
        function(response) {
          if (response.result == 1) {
            let _data = response.data;
            that.commercialList = _data.data;
            that.commercialList.map((item,index) => {
              that.$set(item, 'checked', false);
            });
          } else {
            Toast(response.msg);
          }
        },
        function(response) {
          console.log(response);
        }
      );
    },
    delteteItem(item, i) {
      let that = this;
      let api = this.collectIndex === 0 ?"member.member-favorite.destroy":"plugin.coupon-store.frontend.collection.destroy";
      // i: this.fun.getKeyByI(), type: this.fun.getTyep() 
      let json = {};
      if(this.collChecked) {
        json.has_all = true;
      } else {
        if(this.collectIndex === 0 ) {
          this.collectIdArr.length == 0?json.goods_id = item.goods?.id:json.goods_id = this.collectIdArr;
        } else {
          this.commIdArr.length == 0?json.store_id = item.id:json.store_id = this.commIdArr;
        }
        
      }
      $http.post(api, json, "处理中...").then(
        function(response) {
          if (response.result == 1) {
            // that.collChecked?:that.datas.splice(i, 1);;
            that.collectIdArr = []
            that.getData();
            that.collChecked?that.checkAll = false:'';
            Toast("操作成功");
          } else {
            Toast(response.msg);
          }
        },
        function(response) {
          console.log(response);
        }
      );
    },

    toItem(goods) {
      //console.log(goods);
      //this.$router.push({ name: 'goods', params: { id: goods.id } ,query:{i:this.toi}});
      this.$router.push(this.fun.getUrl("goods", { id: goods.id }));
    },
    commercialTo(storeId) {
      this.$router.push(this.fun.getUrl("commodity", {}, { storeId: storeId }));
    }

    // handleCheckAllChange(event) {

    //   this.checkList = event.target.checked ? this.datasoruce : [];

    // },
    // handleCheckedChange(event) {
    //   console.log(event);

    //   // let checkedCount = value.length;
    //   // this.checkAll = checkedCount === this.datasoruce.length;

    // },

    // },
    // delteteAll() {
    //   if (this.checkList.length == this.datasoruce.length) {

    //   } else {
    //     return;

    //   }

    // }
  },
  components: { cTitle }
};
